﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Editor;
using ITAP.ShprossesBuilder.Calculations;
using ITAP.ShprossesBuilder.Properties;
using ITAP.Core.Modelling.Primitives;
using ITAP.Core.Modelling;
using ITAP.Core.Calculations;

namespace ITAP.ShprossesBuilder.Operations
{
    public enum BarBorder
    {
        ByInner,
        ByOuter,
        ByModelCell
    }

    public class OperationCreateShprossesOnShprossConnector : IOperation
    {
        #region IOperation Members

        public string Name
        {
            get { return "Надеть профили на соединитель"; }
        }

        public OperationGroup OperationGroup
        {
            get { return OperationGroup.ShprossConnectorOperation; }
        }

        public void Execute(ModelControl modelControl)
        {
            SelectMaterialForm materialForm = new SelectMaterialForm();
            materialForm.Initialize(modelControl.ProfileProvider, new Material[] { Material.Shpross }, true, "Выберите профиль", true);
            if (materialForm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;

            foreach (IComponent innerComponent in modelControl.ModelContainer.AllInnerComponents)
            {
                if (innerComponent is ShprossConnector)
                {
                    ShprossConnector shprossConnector = innerComponent as ShprossConnector;
                    if (shprossConnector.Selected)
                    {
                        Model model = (Model)shprossConnector.Parent;
                        foreach (IComponent shprossConnectorInnerComponent in shprossConnector.InnerComponents)
                        {
                            if (shprossConnectorInnerComponent.GetType() == typeof(Connector))
                            {
                                Connector connector = shprossConnectorInnerComponent as Connector;
                                if (connector.EndConnectedComponent != null)
                                    continue;

                                ModelVector tangent = connector.Primitives[0].GetTangent(0);
                                tangent = tangent.Multiply(materialForm.ProfileLength);
                                ModelPoint endPoint = ModelCalculations.GetOffsetPoint(connector.StartPoint, tangent);
                                Shpross shpross = (Shpross)(materialForm.SelectedComponent as Shpross).Clone();
                                shpross.MoveStartPointTo(connector.StartPoint);
                                shpross.MoveEndPointTo(endPoint);                                
                                model.AddInnerComponent(shpross);

                                if (materialForm.EndsToSpacer && shpross.EndConnectedComponent is SpacerConnector)
                                    ConnectionProvider.Connect(shpross.EndConnectedComponent as IEndedComponent, model.Spacer, EndType.Start);
                            }
                        }
                    }
                }
            }
            modelControl.UpdateModel();
        }

        #endregion

        #region IOperation Members


        public System.Drawing.Image Thumbnail
        {
            get { return Resources.CreateShprossesOnConnectors.ToBitmap(); }
        }

        #endregion

        #region IOperation Members


        public bool CanUndo
        {
            get { return true; }
        }

        #endregion
    }
}
